do "E:\yungyu\lottery_baby\do\set_environment.do"

forv i = 97(1)109{
	use "E:\yungyu\boundary_disc\wdata\jhq\jhq_cleaned_`i'.dta", clear
	keep idn year born_date res_hsn_cd res_town_cd res_vill_cd
	loc j = `i'-18
	loc k = `i'-17
	keep if inrange(born_date,"0`j'0902","0`k'0901")
	loc l = `i'+1
	
	merge 1:m idn using "E:\yungyu\boundary_disc\wdata\ibr\ibrt413_`l'_undergrad.dta"
	drop if _m == 2
	drop _m
	merge m:1 idn using "$wdata/education_`i'.dta"
	drop if _m == 2
	drop _m
	
	gen top1 = schl_id == "0003"
	
	gen top5 = 0
	replace top5 = 1 if schl_id == "0001"
	replace top5 = 1 if schl_id == "0002"
	replace top5 = 1 if schl_id == "0003"
	replace top5 = 1 if schl_id == "0005"
	replace top5 = 1 if schl_id == "0007"
	
	gen top10 = 0
	replace top10 = 1 if top5 == 1
	replace top10 = 1 if schl_id == "0004"
	replace top10 = 1 if schl_id == "0008"
	replace top10 = 1 if schl_id == "0009"
	replace top10 = 1 if schl_id == "0016"
	replace top10 = 1 if schl_id == "0022"

	gen top20 = 0
	replace top20 = 1 if top10 == 1
	replace top20 = 1 if schl_id == "0006"
	replace top20 = 1 if schl_id == "0013"
	replace top20 = 1 if schl_id == "0017"
	replace top20 = 1 if schl_id == "0025"
	replace top20 = 1 if schl_id == "1003"
	replace top20 = 1 if schl_id == "1005"
	replace top20 = 1 if schl_id == "1007"
	replace top20 = 1 if schl_id == "1009"
	replace top20 = 1 if schl_id == "1010"
	replace top20 = 1 if schl_id == "1028"
	
	gen cohort = `j'
	replace year = `l'
	gen count = 1
	collapse (mean)top1 top5 top10 top20 oversea (sum)count, by(year cohort res_hsn_cd res_town_cd res_vill_cd)
	
	save "$wdata/top1_prob_`l'.dta", replace
}

clear
forv i = 98(1)110{
	 ap using "$wdata/top1_prob_`i'.dta"
}

collapse (mean)top1 top5 top10 top20 oversea, by(res_hsn_cd res_town_cd res_vill_cd)
save "$wdata/top1_prob_village.dta", replace

clear
forv i = 98(1)110{
	 ap using "$wdata/top1_prob_`i'.dta"
}

collapse (mean)top1 top5 top10 top20 oversea, by(res_hsn_cd res_town_cd)
save "$wdata/top1_prob_township.dta", replace
